package ui;

import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

// 登录界面
public class LoginJFrame extends JFrame implements KeyListener, java.awt.event.ActionListener{
    // 创建一个集合来存储用户信息
    static ArrayList<User> allUser = new ArrayList<>();
    static {
        allUser.add(new User("zhangsan","123"));
        allUser.add(new User("lisi","324"));
    }
    JButton register = new JButton();
    JButton login = new JButton();
    public LoginJFrame() throws ClassNotFoundException, IOException{
        // 读取本地文件中的用户信息
        readUserInfo();
        initJFrame();
        initView();
        // 设置是否可见
        this.setVisible(true);
    }
    // 从本地文件读取用户信息
    public void readUserInfo() throws ClassNotFoundException, IOException{
        // 从本地存储用户的文件中读取用户数据
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Data/user.txt"));
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Data/user.txt"));
        // 写入数据
        User s = new User("wangwu","49");
        // 将读取出来的用户添加到用户列表中
        // User s;
        // while((s = (User)ois.readObject()) != null){
        //     System.out.println(s);
        // }
        // ois.close();
        oos.writeObject(s);
        oos.close();

    }
    // 在这个界面中添加内容
    public void initView(){
        //1. 添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("./image\\login\\用户名.png"));
        usernameText.setBounds(116, 135, 47, 17);
        this.getContentPane().add(usernameText);
        
        // 添加用户名输入框
        JTextField username = new JTextField();
        username.setBounds(195,134,200,30);
        this.getContentPane().add(username);

        //3.添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("./image\\login\\密码.png"));
        passwordText.setBounds(130, 195, 32, 16);
        this.getContentPane().add(passwordText);
        //4.密码输入框
        JTextField password = new JTextField();
        password.setBounds(195, 195, 200, 30);
        this.getContentPane().add(password);
        //验证码提示
        JLabel codeText = new JLabel(new ImageIcon("./image\\login\\验证码.png"));
        codeText.setBounds(133, 256, 50, 30);
        this.getContentPane().add(codeText);

        //验证码的输入框
        JTextField code = new JTextField();
        code.setBounds(195, 256, 100, 30);
        this.getContentPane().add(code);

        String codeStr = CodeUtil.getCode();
        JLabel rightCode = new JLabel();
        //设置内容
        rightCode.setText(codeStr);
        //位置和宽高
        rightCode.setBounds(300, 256, 50, 30);
        //添加到界面
        this.getContentPane().add(rightCode);

        //5.添加登录按钮
        login.setBounds(123, 310, 128, 47);
        login.setIcon(new ImageIcon("./image\\login\\登录按钮.png"));
        //去除按钮的默认边框
        login.setBorderPainted(false);
        //去除按钮的默认背景
        login.setContentAreaFilled(false);
        this.getContentPane().add(login);

        //6.添加注册按钮
        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon("./image\\login\\注册按钮.png"));
        //去除按钮的默认边框
        register.setBorderPainted(false);
        //去除按钮的默认背景
        register.setContentAreaFilled(false);
        this.getContentPane().add(register);

        //7.添加背景图片
        JLabel background = new JLabel(new ImageIcon("./image\\login\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
        // 给两个按钮添加事件
        login.addActionListener(this);
        register.addActionListener(this);
    }
    //要展示用户名或密码错误
    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }
    
    public void initJFrame() {
        this.setSize(488, 430);//设置宽高
        this.setTitle("拼图游戏 V1.0登录");//设置标题
        this.setDefaultCloseOperation(3);//设置关闭模式
        this.setLocationRelativeTo(null);//居中
        this.setAlwaysOnTop(true);//置顶
        this.setLayout(null);//取消内部默认布局
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'keyTyped'");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'keyPressed'");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'keyReleased'");
    }
    

    @Override
    // 触发控件
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if (o == login){
            System.out.println("登录");
        }else if( o == register){
            System.out.println("注册");
        }
    }
    
}
